﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Redis示例
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //http://www.cnblogs.com/yangecnu/p/Introduct-Redis-in-DotNET.html
            try
            {
                Button1_Click(this, null);

            }
            catch (Exception ex){ MessageBox.Show(""+ex);  }

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            string ConnectionString = "";
            ConnectionString =
    @"DRIVER=MapR Drill ODBC Driver;
            AdvancedProperties={CastAnyToVarchar=true;HandshakeTimeout=5;QueryTimeout=180;TimestampTZDisplayTimezone=local;ExcludedSchemas=sys,INFORMATION_SCHEMA;NumberOfPrefetchBuffers=5};
            Catalog=DRILL;
            Schema=;
            AuthenticationType=No Authentication;
            ConnectionType=Direct;
            Host=192.168.2.113;
            Port=31010;
            ";
            //ConnectionString = "DRIVER=MapR Drill ODBC Driver;Catalog=DRILL;Schema=hbase;ConnectionType=Direct;Host=192.168.2.113;Port=31010";
            ConnectionString = "DRIVER=MapR Drill ODBC Driver; AdvancedProperties ={ CastAnyToVarchar = true; HandshakeTimeout = 5; QueryTimeout = 1800; TimestampTZDisplayTimezone = local; ExcludedSchemas = sys,INFORMATION_SCHEMA; NumberOfPrefetchBuffers = 5}; Catalog = DRILL; Schema =; AuthenticationType = No Authentication; ConnectionType = ZooKeeper; ZKQuorum = dataNode04:2181,dataNode03: 2181,dataNode02: 2181,nameNode: 2181; ZKClusterID = drillbits1; ";

            System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(ConnectionString);
            conn.Open();
            //String sql = "SELECT\n"
            //       + "  count(*)  \n"
            //       + "FROM\n"
            //       + "  hbase.`Dat_DataWareHouse` t   ";
            var cmd = conn.CreateCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "SELECT CONVERT_FROM(row_key, 'UTF8'),CONVERT_FROM(Dat_DataWareHouse.Data.Collections, 'UTF8') FROM `hbase`.`Dat_DataWareHouse` limit 100 ";
            System.Data.Odbc.OdbcDataAdapter ad = new System.Data.Odbc.OdbcDataAdapter(cmd);
            DataSet myds = new DataSet();
            ad.Fill(myds);

            MessageBox.Show("查询成功" + myds.Tables[0].Rows.Count);


        }


        public void aa()
        {

            ////redis服务IP和端口
            using (ServiceStack.Redis.RedisClient redisClient = new ServiceStack.Redis.RedisClient("192.168.2.110 ", 6379) { })
            {
                int length = 1 * 10000;
                int num = length;




                Action put = () =>
                {

                    for (int i = 0; i < length; i++)
                    {

                        redisClient.Add<string>(i.ToString(), txt);

                    }
                };

                Action get = () =>
                {

                    for (int i = 0; i < length; i++)
                    {
                        string xx = redisClient.Get<string>(i.ToString());
                    }
                };




                //添加处理10000条用时: 31764毫秒,314.82 / s; ; 运行时间 31,764ms CPU时钟周期28,867,187,226 ,           ,垃圾回收(1代663, 2代4, 3代1)
                //运行时间 31,764ms CPU时钟周期28,867,187,226 ,           ,垃圾回收(1代663, 2代4, 3代1)

                //获取处理10000条用时: 18673毫秒,535.53 / s; ; 运行时间 18,673ms CPU时钟周期22,515,815,392 ,           ,垃圾回收(1代2, 2代0, 3代0)
                //运行时间 18,673ms CPU时钟周期22,515,815,392 ,           ,垃圾回收(1代2, 2代0, 3代0)

                string msg = string.Empty;
                long mm2 = put.StopwatchCode(out msg);
                System.Diagnostics.Trace.WriteLine(string.Format("添加处理{0}条用时:{1}毫秒,{2:f2}/s;;{3}", num, mm2, num / (mm2 / 1000.0), msg));
                long mm = get.StopwatchCode(out msg);
                System.Diagnostics.Trace.WriteLine(string.Format("获取处理{0}条用时:{1}毫秒,{2:f2}/s;;{3}", num, mm, num / (mm / 1000.0), msg));


            }



        }

        /// <summary>
        /// 出错,有问题
        /// </summary>
        public void bb()//StackExchange.Redis
        {

            using (var muxer = StackExchange.Redis.ConnectionMultiplexer.Connect("192.168.2.110:6379"))
            {
                muxer.PreserveAsyncOrder = true;
                StackExchange.Redis.RedisKey key = "456";
                var conn = muxer.GetDatabase();


                //读取数据
              var ss=  conn.HashGetAll("RT");




                int length = 1 * 10000;
                int num = length;



                long xxx = 0;
                Action put = () =>
                {

                    for (int i = 0; i < length; i++)
                    {
                        string dd = i.ToString();
                        conn.KeyDelete(dd);


                        conn.SetAdd(dd, txt);
                        xxx++;
                    }
                };

                Action get = () =>
                {

                    for (int i = 0; i < length; i++)
                    {
                        string xx = conn.StringGet(i.ToString());
                    }
                };
                try
                {

                    string msg = string.Empty;
                    long mm2 = put.StopwatchCode(out msg);
                    System.Diagnostics.Trace.WriteLine(string.Format("添加处理{0}条用时:{1}毫秒,{2:f2}/s;;{3}", num, mm2, num / (mm2 / 1000.0), msg));
                    long mm = get.StopwatchCode(out msg);
                    System.Diagnostics.Trace.WriteLine(string.Format("获取处理{0}条用时:{1}毫秒,{2:f2}/s;;{3}", num, mm, num / (mm / 1000.0), msg));

                }
                catch (Exception d) { throw d; }

            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            aa();
        }

        #region txt
        string txt = @"远程地址: 远程端口: 手动重联
输入数据: 
发送随机数据
服务器 183.238.226.154:4626 连接成功
84895、(2016-03-09 18:09:27.7057)终端号50025155,当前命令列表长度0
84894、(2016-03-09 18:09:27.7057)终端号50015540,当前命令列表长度0
84893、(2016-03-09 18:09:27.7057)终端号50009620,当前命令列表长度0
84892、(2016-03-09 18:09:22.6897)终端号50025155,当前命令列表长度0
84891、(2016-03-09 18:09:22.6897)终端号50015540,当前命令列表长度0
84890、(2016-03-09 18:09:22.6897)终端号50009620,当前命令列表长度0
1条,历史数据提交完成用时:0.0156099秒;64.06/s
84889、(2016-03-09 18:09:21.3614)提交 [ 1 ] 条历史数据.
84888、(2016-03-09 18:09:21.3302)开始提交历史数据. 1
84887、(2016-03-09 18:09:17.6736)终端号50025155,当前命令列表长度0
84886、(2016-03-09 18:09:17.6736)终端号50015540,当前命令列表长度0
84885、(2016-03-09 18:09:17.6736)终端号50009620,当前命令列表长度0
84884、(2016-03-09 18:09:12.6576)终端号50025155,当前命令列表长度0
84883、(2016-03-09 18:09:12.6576)终端号50015540,当前命令列表长度0
84882、(2016-03-09 18:09:12.6576)终端号50009620,当前命令列表长度0
3条,历史数据提交完成用时:0秒;正无穷大/s
84881、(2016-03-09 18:09:11.3138)提交 [ 3 ] 条历史数据.
84880、(2016-03-09 18:09:11.3138)开始提交历史数据. 3
84879、(2016-03-09 18:09:07.6416)终端号50025155,当前命令列表长度0
84878、(2016-03-09 18:09:07.6416)终端号50015540,当前命令列表长度0
84877、(2016-03-09 18:09:07.6416)终端号50009620,当前命令列表长度0
84876、(2016-03-09 18:09:02.6256)终端号50025155,当前命令列表长度0
84875、(2016-03-09 18:09:02.6256)终端号50015540,当前命令列表长度0
84874、(2016-03-09 18:09:02.6256)终端号50009620,当前命令列表长度0
OTA:TCP客户端:0,接收:1430,0.00/s;待解析:0,0.00/s;已解析:1430;待处理:0,0.00/s;已处理:1427;待记录:0,0.00/s,已记录:1430;UDP接收:835,0.07/s;;解析=0,处理=0,故障=0,在线=0,接收池=59998/60000,处理池＝0/0,日志=1000,待入库:0,需入库:3429,已入库:3429,实时转历史:3429,登录包:15
1条,历史数据提交完成用时:0秒;正无穷大/s
84873、(2016-03-09 18:09:01.2974)提交 [ 1 ] 条历史数据.
84872、(2016-03-09 18:09:01.2974)开始提交历史数据. 1
84871、(2016-03-09 18:08:57.6096)终端号50025155,当前命令列表长度0
84870、(2016-03-09 18:08:57.6096)终端号50015540,当前命令列表长度0
84869、(2016-03-09 18:08:57.6096)终端号50009620,当前命令列表长度0
84868、(2016-03-09 18:08:52.5935)终端号50025155,当前命令列表长度0
84867、(2016-03-09 18:08:52.5935)终端号50015540,当前命令列表长度0
84866、(2016-03-09 18:08:52.5935)终端号50009620,当前命令列表长度0
1条,历史数据提交完成用时:0秒;正无穷大/s
84865、(2016-03-09 18:08:51.2809)提交 [ 1 ] 条历史数据.
84864、(2016-03-09 18:08:51.2809)开始提交历史数据. 1
84863、(2016-03-09 18:08:47.5768)终端号50025155,当前命令列表长度0
84862、(2016-03-09 18:08:47.5768)终端号50015540,当前命令列表长度0
84861、(2016-03-09 18:08:47.5768)终端号50009620,当前命令列表长度0
84860、(2016-03-09 18:08:42.5608)终端号50025155,当前命令列表长度0
84859、(2016-03-09 18:08:42.5608)终端号50015540,当前命令列表长度0
84858、(2016-03-09 18:08:42.5608)终端号50009620,当前命令列表长度0
3条,历史数据提交完成用时:0.0312529秒;95.99/s
84857、(2016-03-09 18:08:41.2937)提交 [ 3 ] 条历史数据.
84856、(2016-03-09 18:08:41.2781)开始提交历史数据. 3
84855、(2016-03-09 18:08:37.5434)终端号50025155,当前命令列表长度0
84854、(2016-03-09 18:08:37.5434)终端号50015540,当前命令列表长度0
84853、(2016-03-09 18:08:37.5434)终端号50009620,当前命令列表长度0
84852、(2016-03-09 18:08:32.5263)终端号50025155,当前命令列表长度0
84851、(2016-03-09 18:08:32.5263)终端号50015540,当前命令列表长度0
84850、(2016-03-09 18:08:32.5263)终端号50009620,当前命令列表长度0
OTA:TCP客户端:0,接收:1430,0.00/s;待解析:0,0.00/s;已解析:1430;待处理:0,0.00/s;已处理:1427;待记录:0,0.00/s,已记录:1430;UDP接收:833,0.07/s;;解析=0,处理=0,故障=0,在线=0,接收池=59998/60000,处理池＝0/0,日志=1000,待入库:0,需入库:3424,已入库:3424,实时转历史:3424,登录包:15
1条,历史数据提交完成用时:0秒;正无穷大/s
84849、(2016-03-09 18:08:31.2605)提交 [ 1 ] 条历史数据.
84848、(2016-03-09 18:08:31.2605)开始提交历史数据. 1
84847、(2016-03-09 18:08:27.5103)终端号50025155,当前命令列表长度0
84846、(2016-03-09 18:08:27.5103)终端号50015540,当前命令列表长度0
84845、(2016-03-09 18:08:27.5103)终端号50009620,当前命令列表长度0
84844、(2016-03-09 18:08:22.4931)终端号50025155,当前命令列表长度0
84843、(2016-03-09 18:08:22.4931)终端号50015540,当前命令列表长度0
84842、(2016-03-09 18:08:22.4931)终端号50009620,当前命令列表长度0
1条,历史数据提交完成用时:0秒;正无穷大/s
84841、(2016-03-09 18:08:21.2404)提交 [ 1 ] 条历史数据.
84840、(2016-03-09 18:08:21.2404)开始提交历史数据. 1
84839、(2016-03-09 18:08:17.4807)终端号50025155,当前命令列表长度0
84838、(2016-03-09 18:08:17.4807)终端号50015540,当前命令列表长度0
84837、(2016-03-09 18:08:17.4807)终端号50009620,当前命令列表长度0
84836、(2016-03-09 18:08:12.4745)终端号50025155,当前命令列表长度0
84835、(2016-03-09 18:08:12.4745)终端号50015540,当前命令列表长度0
84834、(2016-03-09 18:08:12.4745)终端号50009620,当前命令列表长度0
3条,历史数据提交完成用时:0秒;正无穷大/s
84833、(2016-03-09 18:08:11.2234)提交 [ 3 ] 条历史数据.
84832、(2016-03-09 18:08:11.2234)开始提交历史数据. 3
84831、(2016-03-09 18:08:07.4666)终端号50025155,当前命令列表长度0
84830、(2016-03-09 18:08:07.4666)终端号50015540,当前命令列表长度0
84829、(2016-03-09 18:08:07.4666)终端号50009620,当前命令列表长度0
84828、(2016-03-09 18:08:02.4505)终端号50025155,当前命令列表长度0
84827、(2016-03-09 18:08:02.4505)终端号50015540,当前命令列表长度0
84826、(2016-03-09 18:08:02.4505)终端号50009620,当前命令列表长度0
OTA:TCP客户端:0,接收:1430,0.00/s;待解析:0,0.00/s;已解析:1430;待处理:0,0.00/s;已处理:1427;待记录:0,0.00/s,已记录:1430;UDP接收:831,0.07/s;;解析=0,处理=0,故障=0,在线=0,接收池=59998/60000,处理池＝0/0,日志=1000,待入库:0,需入库:3419,已入库:3419,实时转历史:3419,登录包:15
1条,历史数据提交完成用时:0秒;正无穷大/s
84825、(2016-03-09 18:08:01.2005)提交 [ 1 ] 条历史数据.
84824、(2016-03-09 18:08:01.2005)开始提交历史数据. 1
84823、(2016-03-09 18:07:58.5440)读取配置文件InCom.OTMForwarding.config
84822、(2016-03-09 18:07:57.4345)终端号50025155,当前命令列表长度0
84821、(2016-03-09 18:07:57.4345)终端号50015540,当前命令列表长度0
84820、(2016-03-09 18:07:57.4345)终端号50009620,当前命令列表长度0
84819、(2016-03-09 18:07:52.4212)终端号50025155,当前命令列表长度0
84818、(2016-03-09 18:07:52.4212)终端号50015540,当前命令列表长度0
84817、(2016-03-09 18:07:52.4212)终端号50009620,当前命令列表长度0
1条,历史数据提交完成用时:0秒;正无穷大/s
84816、(2016-03-09 18:07:51.1826)提交 [ 1 ] 条历史数据.
84815、(2016-03-09 18:07:51.1826)开始提交历史数据. 1
84814、(2016-03-09 18:07:47.4032)终端号50025155,当前命令列表长度0
84813、(2016-03-09 18:07:47.4032)终端号50015540,当前命令列表长度0
84812、(2016-03-09 18:07:47.4032)终端号50009620,当前命令列表长度0
84811、(2016-03-09 18:07:42.3946)终端号50025155,当前命令列表长度0
84810、(2016-03-09 18:07:42.3946)终端号50015540,当前命令列表长度0
84809、(2016-03-09 18:07:42.3946)终端号50009620,当前命令列表长度0
3条,历史数据提交完成用时:0秒;正无穷大/s
84808、(2016-03-09 18:07:41.1581)提交 [ 3 ] 条历史数据.
84807、(2016-03-09 18:07:41.1581)开始提交历史数据. 3
84806、(2016-03-09 18:07:37.3848)终端号50025155,当前命令列表长度0
84805、(2016-03-09 18:07:37.3848)终端号50015540,当前命令列表长度0
84804、(2016-03-09 18:07:37.3848)终端号50009620,当前命令列表长度0
84803、(2016-03-09 18:07:32.3680)终端号50025155,当前命令列表长度0
84802、(2016-03-09 18:07:32.3680)终端号50015540,当前命令列表长度0
84801、(2016-03-09 18:07:32.3680)终端号50009620,当前命令列表长度0
OTA:TCP客户端:0,接收:1430,0.00/s;待解析:0,0.00/s;已解析:1430;待处理:0,0.00/s;已处理:1427;待记录:0,0.00/s,已记录:1430;UDP接收:829,0.07/s;;解析=0,处理=0,故障=0,在线=0,接收池=59998/60000,处理池＝0/0,日志=1000,待入库:0,需入库:3414,已入库:3414,实时转历史:3414,登录包:15
1条,历史数据提交完成用时:0.0156229秒;64.01/s
84800、(2016-03-09 18:07:31.1456)提交 [ 1 ] 条历史数据.
84799、(2016-03-09 18:07:31.1300)开始提交历史数据. 1
84798、(2016-03-09 18:07:27.3591)终端号50025155,当前命令列表长度0
84797、(2016-03-09 18:07:27.3591)终端号50015540,当前命令列表长度0
84796、(2016-03-09 18:07:27.3591)终端号50009620,当前命令列表长度0
84795、(2016-03-09 18:07:22.3429)终端号50025155,当前命令列表长度0
84794、(2016-03-09 18:07:22.3429)终端号50015540,当前命令列表长度0
84793、(2016-03-09 18:07:22.3429)终端号50009620,当前命令列表长度0
1条,历史数据提交完成用时:0秒;正无穷大/s
84792、(2016-03-09 18:07:21.1084)提交 [ 1 ] 条历史数据.
84791、(2016-03-09 18:07:21.1084)开始提交历史数据. 1
84790、(2016-03-09 18:07:17.3244)终端号50025155,当前命令列表长度0
84789、(2016-03-09 18:07:17.3244)终端号50015540,当前命令列表长度0
84788、(2016-03-09 18:07:17.3244)终端号50009620,当前命令列表长度0
84787、(2016-03-09 18:07:12.3084)终端号50025155,当前命令列表长度0
84786、(2016-03-09 18:07:12.3084)终端号50015540,当前命令列表长度0
84785、(2016-03-09 18:07:12.3084)终端号50009620,当前命令列表长度0
3条,历史数据提交完成用时:0秒;正无穷大/s
84784、(2016-03-09 18:07:11.0896)提交 [ 3 ] 条历史数据.
84783、(2016-03-09 18:07:11.0896)开始提交历史数据. 3
84782、(2016-03-09 18:07:07.2924)终端号50025155,当前命令列表长度0
84781、(2016-03-09 18:07:07.2924)终端号50015540,当前命令列表长度0
84780、(2016-03-09 18:07:07.2924)终端号50009620,当前命令列表长度0
84779、(2016-03-09 18:07:02.2764)终端号50025155,当前命令列表长度0
84778、(2016-03-09 18:07:02.2764)终端号50015540,当前命令列表长度0
84777、(2016-03-09 18:07:02.2764)终端号50009620,当前命令列表长度0
OTA:TCP客户端:0,接收:1430,0.00/s;待解析:0,0.00/s;已解析:1430;待处理:0,0.00/s;已处理:1427;待记录:0,0.00/s,已记录:1430;UDP接收:827,0.07/s;;解析=0,处理=0,故障=0,在线=0,接收池=59998/60000,处理池＝0/0,日志=1000,待入库:0,需入库:3409,已入库:3409,实时转历史:3409,登录包:15
1条,历史数据提交完成用时:0秒;正无穷大/s
84776、(2016-03-09 18:07:01.0731)提交 [ 1 ] 条历史数据.
84775、(2016-03-09 18:07:01.0731)开始提交历史数据. 1
84774、(2016-03-09 18:06:57.2603)终端号50025155,当前命令列表长度0
84773、(2016-03-09 18:06:57.2603)终端号50015540,当前命令列表长度0
84772、(2016-03-09 18:06:57.2603)终端号50009620,当前命令列表长度0
84771、(2016-03-09 18:06:52.2439)终端号50025155,当前命令列表长度0
84770、(2016-03-09 18:06:52.2439)终端号50015540,当前命令列表长度0
84769、(2016-03-09 18:06:52.2439)终端号50009620,当前命令列表长度0
1条,历史数据提交完成用时:0秒;正无穷大/s
84768、(2016-03-09 18:06:51.0563)提交 [ 1 ] 条历史数据.
84767、(2016-03-09 18:06:51.0563)开始提交历史数据. 1
84766、(2016-03-09 18:06:47.2279)终端号50025155,当前命令列表长度0
84765、(2016-03-09 18:06:47.2279)终端号50015540,当前命令列表长度0
84764、(2016-03-09 18:06:47.2279)终端号50009620,当前命令列表长度0
2";
        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            bb();
        }

        private void Drill读取一个表所有的数据(object sender, RoutedEventArgs e)
        {
            System.Threading.Tasks.Task.Factory.StartNew(() => {
            try
            {
                string ConnectionString = "";
                ConnectionString =
                @"DRIVER=MapR Drill ODBC Driver;
                    AdvancedProperties={CastAnyToVarchar=true;HandshakeTimeout=5;QueryTimeout=180;TimestampTZDisplayTimezone=local;ExcludedSchemas=sys,INFORMATION_SCHEMA;NumberOfPrefetchBuffers=5};
                    Catalog=DRILL;
                    Schema=;
                    AuthenticationType=No Authentication;
                    ConnectionType=Direct;
                    Host=192.168.2.113;
                    Port=31010;
                    ";

                System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(ConnectionString);
                conn.Open();
                //String sql = "SELECT\n"
                //       + "  count(*)  \n"
                //       + "FROM\n"
                //       + "  hbase.`Dat_DataWareHouse` t   ";
                var cmd = conn.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "SELECT CONVERT_FROM(row_key, 'UTF8') as key,CONVERT_FROM(Dat_DataWareHouse.Data.Collections, 'UTF8') as val FROM `hbase`.`Dat_DataWareHouse` ";
                var reader = cmd.ExecuteReader();
                while (Read(reader)) { }
                MessageBox.Show("读取全表完成共" + ont);

            }
            catch (Exception ex) { Dispatcher.BeginInvoke(new Action(()=> { MessageBox.Show("" + ex); }));  }
            });
           
        }

        long ont = 0;

        /// <summary>
        /// 读取数据信息
        /// </summary>
        /// <param name="reader"></param>
        public bool Read(System.Data.Odbc.OdbcDataReader reader) {

            bool result = false;

            try {
                if (result=reader.Read())
                {
                    System.Threading.Interlocked.Increment(ref ont);
                }

            }
            catch (Exception ex) {
                Console.WriteLine(ex);
            }


            return result;

        }
    }
}
